package com.adl.hotelmanage.servlet;

import java.io.IOException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.adl.hotelmanage.util.DbUtil;
import com.adl.hotelmanage.po.RoomInfo;
import com.adl.hotelmanage.po.room;

/**
 * Servlet implementation class firstPageServlet
 */
@WebServlet("/firstPageServlet")
public class firstPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public firstPageServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	      
          try {      
        	  Connection conn = DbUtil.getSQLConn();
        	 PreparedStatement ptmt = null;
         String sql= "select *	from t_roominfo"; 
     	ptmt = conn.prepareStatement(sql);
           ptmt.executeQuery();
      	 List<RoomInfo> roomlist = new ArrayList<RoomInfo>(); 
      	ResultSet resultSet = ptmt.executeQuery(); 
       	 
	      while (resultSet.next()) { 
	    	  RoomInfo roominfo = new RoomInfo();
				roominfo.setRoomid(resultSet.getString("roominfo_roomId"));
				roominfo.setRoomtype(resultSet.getString("roominfo_roomtype"));
				roominfo.setState(resultSet.getString("roominfo_state"));
				roominfo.setLocation(resultSet.getString("roominfo_location"));
				roominfo.setNote(resultSet.getString("roominfo_note"));
				roominfo.setBednum(Integer.parseInt(resultSet.getString("roominfo_bednum")));
				roominfo.setPrice(Integer.parseInt(resultSet.getString("roominfo_price")));
	              roomlist.add(roominfo); 
	     
  } 
	      request.setAttribute("roomlist", roomlist); 
	      resultSet.close(); 
	     ptmt.close(); 
	      conn.close(); 
	  
	    } catch (Exception e) { 
	      e.printStackTrace(); 
	    } 
	  
	    request.getRequestDispatcher("welcome.jsp").forward(request, response); 

}	
}
